package ar.com.larctest.forms;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import ar.com.larctest.R;
import ar.com.larctest.adapters.ListViewMasterDataHorsesAdapter;
import ar.com.larctest.dbHelper.SQLLARCTESTHelper;
import ar.com.larctest.entities.Horses;
import ar.com.larctest.entities.temp.TMeasurementItem;
import ar.com.larctest.providers.HorsesProvider;
import ar.com.larctest.providers.temp.MeasurementTempProvider;
import ar.com.larctest.utils.Constants;

public class PopUpListHorsesActivity extends Activity {
    private static final String TAG = "PopUpListHorsesActivity";

    private ListViewMasterDataHorsesAdapter horsesAdapter;
    private List<Horses> lstHorses = new ArrayList<Horses>();

    ListView listViewHorsesData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pop_up_list_horses);

        SQLLARCTESTHelper sqlHelper = SQLLARCTESTHelper.getHelper(this);
        sqlHelper.dbOpen();

        try
        {
            listViewHorsesData = (ListView) findViewById(R.id.lvHorses);

            HorsesProvider horsesProvider = new HorsesProvider(this);
            lstHorses = horsesProvider.getHorsesWidthOutMeasurement();
            horsesAdapter = new ListViewMasterDataHorsesAdapter(PopUpListHorsesActivity.this, R.id.lvHorses, lstHorses);
            View header = getLayoutInflater().inflate(R.layout.listview_header_horses, null);
            listViewHorsesData.addHeaderView(header);

            listViewHorsesData.setAdapter(horsesAdapter);
            listViewHorsesData.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    Horses selectedHorse = lstHorses.get(position - 1);
                    if(selectedHorse.getSelected())
                        selectedHorse.setSelected(false);
                    else
                        selectedHorse.setSelected(true);
                    horsesAdapter.notifyDataSetChanged();
                }
            });


        } catch (Exception e) {
            Log.e(TAG, e.toString());
        }
        finally{
            sqlHelper.dbClose();
        }
    }

    public void onClickAdd(View view)
    {
        try
        {
            Boolean selectAnyHorse = false;
            for(Horses horseItem: lstHorses) {
                if(horseItem.getSelected()) {
                    selectAnyHorse = true;
                    break;
                }
            }

            if(!selectAnyHorse) {
                LayoutInflater inflater = getLayoutInflater();
                View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));

                TextView text = (TextView) layout.findViewById(R.id.text);
                text.setText(getText(R.string.Measurement_Init_msgInputHorses));

                Toast toast = new Toast(getApplicationContext());
                toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setView(layout);
                toast.show();
            }
            else
            {
                MeasurementTempProvider tempProvider = new MeasurementTempProvider(this);

                for(Horses horseItem: lstHorses) {
                    if(horseItem.getSelected()) {
                        TMeasurementItem measurementItem = new TMeasurementItem();
                        measurementItem.setIdHorse(horseItem.getIdHorse());
                        measurementItem.setNameHorse(horseItem.getName());
                        measurementItem.setState(Constants.STATE_INIT);
                        measurementItem.setInitTime(null);
                        measurementItem.setFinishTime(null);
                        measurementItem.setTimeOut1(Constants.INIT_TIMEOUT_1);
                        measurementItem.setTimeOut2(Constants.INIT_TIMEOUT_2);
                        measurementItem.setTimeOut3(Constants.INIT_TIMEOUT_3);
                        measurementItem.setLacticAcid1(0D);
                        measurementItem.setHeartRate1(0D);
                        measurementItem.setLacticAcid2(0D);
                        measurementItem.setHeartRate2(0D);
                        measurementItem.setLacticAcid3(0D);
                        measurementItem.setHeartRate3(0D);
                        measurementItem.setRespiratoryRate3(0D);
                        measurementItem.setSelected(false);

                        tempProvider.insertMeasurementItem(measurementItem);
                    }
                }

                Intent intentMeasurement = new Intent(this, MeasurementActivity.class);
                startActivityForResult(intentMeasurement, 1);
            }
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }
    }

    public void onClickExit(View view)
    {
        setResult(RESULT_CANCELED);
        this.finish();
    }
}
